<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if($_RQ['merchid']){
		$params['merchid']=$_RQ['merchid'];
	}
	$return['params']=$params;
	set_html('title','问答库');
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['merchid']){
		$params['merchid']=$_RQ['merchid'];
	}else{
		$params['workerid']=$_SESSION['workerid'];
	}
	$list=pdo_getslice('jy_aikefu_wenda',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['msg']=cfc('tools')->substr($item['msg'],0,38);
			$item['statustitle']=$item['status']?'已启用':'未启用';
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aikefu_wenda',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$postdata['latlng']=ijson_encode($postdata['latlng']);
	$data=array_fields(array('msg','status','title','merchid','paixu'),$postdata);	
	if(!$data['title']){
		exi('请填写问题','error');
	}
	if(!$data['msg']){
		exi('请填写答案','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aikefu_wenda',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','main');
	}else{
		$merchid = pdo_getcplumn('jy_aikefu_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['workerid']),'merchid');
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['workerid']=$_SESSION['workerid'];
		$data['merchid']=$merchid;
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aikefu_wenda',$data);
		exi('添加成功！','','main');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_aikefu_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_aikefu_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}